// miniprogram/pages/home/index/index.js
import {getDate} from "../../../utils/Util";
import Notify from "@vant/weapp/notify/notify";
import {getStorageSync, setStorage} from "../../../utils/Storage"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        screenShow: false, //筛选
        loadList: {
            useSlot: false,
            params: {complete: 0, verify: [0, 1]},
            loading: true,
            left: [],
            right: []
        }
    },

    ___refreshController: false, //下拉刷新控制器 控制是否可以刷新
    ___pageScrollTimeOut: null,  //页面滚动定时器
    ___pageScrollTop: 0,  //页面滚动高度
    ___windowHeight: wx.getSystemInfoSync().windowHeight || 300,

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /*
    * 筛选组件监听函数
    * */
    onScreen: function (e) {
        let params = e.detail.result;
        this.setData({
            "loadList.params": params,
            "loadList.reset": true //重置数据
        })
    },

    /*
     * 列表加载组件
     * */
    onLoadList: function (e) {
        const that = this;
        let success = e.detail.success;
        let leftList = that.data.loadList.left;
        let rightList = that.data.loadList.right;
        let isReset = that.data.loadList.reset;
        if (success) {
            let newList = e.detail.result.map(function (item) {
                item.createTime = getDate(item.createTime).split(" ")[0];
                return item
            });
            let left = newList.filter(function (item, index) {
                return (index + 1) % 2 > 0
            })
            let right = newList.filter(function (item, index) {
                return (index + 1) % 2 === 0
            });
            if (!isReset) {
                leftList.push.apply(leftList, left);
                rightList.push.apply(rightList, right);
                leftList.length < rightList.length ? leftList.push(rightList.splice(-1, 1)) : null
            } else {
                leftList = left;
                rightList = right;
                that.data.loadList.reset = false;
            }
            that.setData({
                "loadList.left": leftList,
                "loadList.right": rightList,
                "loadList.useSlot": (leftList.length + rightList.length) === 0
            }, function () {
                that.___refreshController = true;
                if (!getStorageSync("config.indexLoadGuide")) {
                    Notify({
                        type: "success",
                        message: "下拉更新内容、上拉加载更多！",
                        duration: 3000,
                        color: "#57bd6a",
                        background: "#fff"
                    })
                    setStorage("config.indexLoadGuide", "1")
                }
            });
        }
    },

    /*
    * 点击栏目跳转
    * */
    bindNavigatorTo: function (e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/pages/user/record-detail/record-detail?id=" + id
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        if (this.___refreshController) {
            this.___refreshController = false;
            this.setData({"loadList.reset": true});
            setTimeout(function () {
                wx.stopPullDownRefresh();
            }, 1500)
        } else {
            wx.stopPullDownRefresh();
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"loadList.loading": true})
    },

    /*
    * 页面滚动
    * */
    onPageScroll: function (e) {
        const that = this;
        clearTimeout(that.___pageScrollTimeOut);
        that.___pageScrollTimeOut = setTimeout(function () {
            if (e.scrollTop > that.___windowHeight / 2) {
                !that.data.screenShow ? that.setData({screenShow: true}) : null
            } else {
                that.data.screenShow ? that.setData({screenShow: false}) : null
            }
        }, 300)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    onShareTimeline: function () {}
})